package com.hcy.orderapi.driver.controller;

import com.hcy.ordermodel.dto.request.*;
import com.hcy.ordermodel.dto.response.*;
import com.hcy.orderbusiness.OptOrderBz;
import com.hcy.rabbitbasicmodel.enums.Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicmodel.signl.Finish;
import com.hcy.rabbitbasicmodel.signl.Update;
import com.hcy.rabbitbasicutils.utils.SnowFlake;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author huchenying
 * @Description 司机端订单服务
 * @Date 2021/12/20 12:21
 */
@RestController
@RequestMapping("/driver")
public class DriverOrderController {

    private static final Logger logger = LoggerFactory.getLogger(DriverOrderController.class);

    @Autowired
    OptOrderBz optOrderBz;

    /**
     * @Author huchenying
     * @Description 绑定轨迹
     * @Date 2021/12/22 15:46
     **/
    @PostMapping("/bindTrack")
    public ResponseData<SimpleOrderResponseDTO> bindTrack(@Validated @RequestBody BindTrackRequestDTO requestDTO) {
        logger.info("bindTrack start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<SimpleOrderResponseDTO> responseData = new ResponseData<>();
        try {
            //接到乘客的更新
            SimpleOrderResponseDTO res = optOrderBz.upDataOrderToStart(requestDTO);
            responseData.setCode(Code.Success);
            responseData.setMsg("binding success!");
            responseData.setData(res);
            return responseData;
        } catch (Exception e) {
            logger.error("订单更新失败！", e);
            responseData.setCode(Code.Fail);
            responseData.setMsg("binding fail!");
            return responseData;
        }
    }

    /**
     * @Author huchenying
     * @Description 获取当前服务的订单和预约单
     * @Date 2021/12/20 12:22
     **/
    @PostMapping("/getSimpleOrder")
    public ResponseData<SimpleOrderResponseDTO> getSimpleOrder(@Validated @RequestBody DriverOrderRequestDTO requestDTO) {
        logger.info("getSimpleOrder start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<SimpleOrderResponseDTO> responseData = null;
        try {
            responseData = optOrderBz.getSimpleOrder(requestDTO.getOrderNumber());
        } catch (Exception e) {
            logger.error("getSimpleOrder error e -> {}", ToStringBuilder.reflectionToString(e));
            responseData = new ResponseData<>();
            responseData.setCode(Code.Fail);
            responseData.setMsg("getSimpleOrder error");
        }
        logger.info("result -> {}", ToStringBuilder.reflectionToString(responseData));
        return responseData;
    }

    /**
     * @Author huchenying
     * @Description 获取订单详情
     * @Date 2021/12/20 12:26
     **/
    @PostMapping("/orderDetails")
    public ResponseData<OrderInfoResponseDTO> orderDetails(@Validated @RequestBody DriverOrderRequestDTO requestDTO) {
        ResponseData<OrderInfoResponseDTO> responseData = new ResponseData<>();
        try {
            OrderInfoResponseDTO responseDTO = optOrderBz.getOrderDetails(requestDTO);
            responseData.setData(responseDTO);
            responseData.setCode(Code.Success);
            return responseData;
        } catch (Exception e) {
            logger.error("订单详情获取失败！", e);
            responseData.setCode(Code.Fail);
            responseData.setMsg("系统异常");
            return responseData;
        }
    }

    /**
     * @Author huchenying
     * @Description 添加订单
     * @Date 2021/12/20 12:27
     **/
    @PostMapping("/addOrder")
    public ResponseData<AddOrderResponseDTO> addOrder(@Validated @RequestBody AddOrderRequestDTO requestDTO) {
        logger.info("addOrder start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<AddOrderResponseDTO> responseData = new ResponseData<>();
        try {
            //uuid订单号
            requestDTO.setOrderNumber(SnowFlake.getId());
            //写死
            requestDTO.setCarSid(538258);
            AddOrderResponseDTO responseDTO = optOrderBz.createOrder(requestDTO);
            responseData.setData(responseDTO);
            responseData.setCode(Code.Success);
            responseData.setMsg("订单添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("订单添加失败！");
            responseData.setCode(Code.Fail);
            responseData.setMsg("订单添加失败！");
        }
        logger.info("res -> {}", ToStringBuilder.reflectionToString(responseData));
        return responseData;
    }

    /**
     * @Author huchenying
     * @Description 更新订单状态
     * @Date 2021/12/23 15:11
     **/
    @PostMapping("/updateOrder")
    public ResponseData<SimpleOrderResponseDTO> updateOrder(@Validated({Update.class}) @RequestBody UpdateOrderRequestDTO requestDTO) {
        logger.info("addOrder start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<SimpleOrderResponseDTO> responseData = new ResponseData<>();
        try {
            SimpleOrderResponseDTO res = optOrderBz.upDataOrderStatus(requestDTO);
            responseData.setCode(Code.Success);
            responseData.setData(res);
        } catch (Exception e) {
            logger.error(e.getMessage());
            responseData.setCode(Code.Fail);
            responseData.setMsg(e.getMessage());
        }
        return responseData;
    }

    /**
     * @Author huchenying
     * @Description 完成订单
     * @Date 2021/12/20 12:43
     **/
    @PostMapping("/finishOrder")
    public ResponseData<SimpleOrderResponseDTO> finishOrder(@Validated({Finish.class}) @RequestBody UpdateOrderRequestDTO requestDTO) {
        logger.info("finishOrder start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<SimpleOrderResponseDTO> responseData = new ResponseData<>();
        try {
            SimpleOrderResponseDTO res = optOrderBz.finishOrder(requestDTO);
            responseData.setCode(Code.Success);
            responseData.setData(res);
            return responseData;
        } catch (Exception e) {
            logger.error("完成订单失败 error -> {}", ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
            responseData.setMsg(e.getMessage());
            return responseData;
        }
    }

    /**
     * @Author huchenying
     * @Description
     * @Date 2021/12/27 15:08
     **/
    @PostMapping("/findDriverHomeInfo")
    public ResponseData<HomeInfoResponseDTO> findDriverHomeInfo(@Validated @RequestBody HomeInfoRequestDTO requestDTO) {
        logger.info("findDriverHomeInfo start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<HomeInfoResponseDTO> responseData = new ResponseData<>();

        try {
            HomeInfoResponseDTO homeInfoResponseDTO = optOrderBz.findDriverHomeInfo(requestDTO);
            responseData.setCode(Code.Success);
            responseData.setData(homeInfoResponseDTO);
        } catch (Exception e) {
            logger.error("获取首页信息失败！", e);
            responseData.setCode(Code.Exception);
            responseData.setMsg("获取首页信息失败");
        }
        logger.info("findDriverHomeInfo end result -> {}", ToStringBuilder.reflectionToString(responseData));
        return responseData;
    }

    /**
     * @Author huchenying
     * @Description 给订单绑定司机信息
     * @Date 2022/3/20 21:46
     **/
    @PostMapping("/addDriverForOrder")
    public ResponseData<SimpleOrderResponseDTO> addDriverForOrder(@Validated @RequestBody AddDriverForOrderRequestDTO requestDTO) {
        logger.info("addDriverForOrder start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<SimpleOrderResponseDTO> responseData = optOrderBz.updateOrderForAddDriver(requestDTO);
        logger.info("addDriverForOrder end param -> {}", responseData);
        return responseData;
    }

    /**
     * @Author huchenying
     * @Description 订单预估
     * @Date 2022/3/20 21:46
     **/
    @PostMapping("/computerOrder")
    public ResponseData<OrderInfoResponseDTO> computerOrder(@Validated @RequestBody DriveComputerOrderRequestDTO requestDTO) {
        logger.info("computerOrder start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<OrderInfoResponseDTO> responseData = null;
        try {
            responseData = optOrderBz.computerOrder(requestDTO);
            responseData.setCode(Code.Success);
        } catch (Exception e) {
            logger.error("订单估算失败 e-> {}", ToStringBuilder.reflectionToString(e));
            responseData = new ResponseData<>();
            responseData.setCode(Code.Fail);
            responseData.setMsg("订单估算失败！");
        }
        logger.info("computerOrder end param -> {}", responseData);
        return responseData;
    }

    /**
     * @Author huchenying
     * @Description getPayOrder
     * @Date 2022/4/7 13:15
     **/
    @PostMapping("/getPayOrder")
    public ResponseData<PayOrderResponseDTO> getPayOrder(@Validated @RequestBody PayOrderRequestDTO requestDTO) {
        logger.info("checkPayOrder start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<PayOrderResponseDTO> responseData = new ResponseData<>();
        try {
            PayOrderResponseDTO responseDTO = optOrderBz.getPayOrder(requestDTO);
            responseData.setData(responseDTO);
            responseData.setCode(Code.Success);
        } catch (Exception e) {
            logger.error("获取交易单异常 e -> {}", ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
        }
        logger.info("res -> {}", ToStringBuilder.reflectionToString(responseData));
        return responseData;
    }

    /**
     * @Author huchenying
     * @Description getPayOrder
     * @Date 2022/4/7 13:15
     **/
    @PostMapping("/getNoPayOrder")
    public ResponseData<PayOrderResponseDTO> getNoPayOrder(@RequestBody PayOrderRequestDTO requestDTO) {
        logger.info("getNoPayOrder start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<PayOrderResponseDTO> responseData = new ResponseData<>();
        try {
            PayOrderResponseDTO responseDTO = optOrderBz.notPayOrder(requestDTO.getCustomerId());
            responseData.setData(responseDTO);
            responseData.setCode(Code.Success);
        } catch (Exception e) {
            logger.error("获取交易单异常 e -> {}", ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
        }
        logger.info("res -> {}", ToStringBuilder.reflectionToString(responseData));
        return responseData;
    }



    /**
     * @Author huchenying
     * @Description 取消订单
     * @Date 2022/4/7 13:15
     **/
    @PostMapping("/cancelOrder")
    public ResponseData<SimpleOrderResponseDTO> driverCancelOrder(@Validated @RequestBody CancelOrderRequestDTO requestDTO) {
        logger.info("driverCancelOrder start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<SimpleOrderResponseDTO> responseData = new ResponseData<>();
        try {
            SimpleOrderResponseDTO responseDTO = optOrderBz.driverCancelOrder(requestDTO.getOrderNumber());
            responseData.setCode(Code.Success);
            responseData.setData(responseDTO);
        } catch (Exception e) {
            logger.error("余额支付异常 e -> {}", ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
            responseData.setMsg("支付失败！");
        }
        logger.info("res -> {}", ToStringBuilder.reflectionToString(responseData));
        return responseData;
    }

    /**
     * @Author huchenying
     * @Description 获取里程单信息
     * @Date 2022/4/15 20:43
     **/
    @PostMapping("/getOrderList")
    public ResponseData<List<OrderInfoResponseDTO>> getOrderList(@Validated @RequestBody DriverOrderListRequestDTO requestDTO){
        logger.info("getOrderList start param -> {}",ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<List<OrderInfoResponseDTO>> responseData = new ResponseData<>();
        try {
            List<OrderInfoResponseDTO> responseDTOList = optOrderBz.getArriveOrderList(requestDTO);
            responseData.setCode(Code.Success);
            responseData.setData(responseDTOList);
        } catch (Exception e) {
           logger.error("e -< {}",ToStringBuilder.reflectionToString(e));
           responseData.setCode(Code.Fail);
           responseData.setMsg("获取已经支付的订单信息错误！");
        }
        return responseData;
    }
}
